﻿using System;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Games.WPF.UI
{
    public class RotateThumb : Thumb
    {
        #region Constructors

        public RotateThumb()
        {
            DragDelta += new DragDeltaEventHandler(RotateThumb_DragDelta);
            DragStarted += new DragStartedEventHandler(RotateThumb_DragStarted);
        }

        #endregion

        #region Properties

        private double InitialAngle { get; set; }
        private Vector StartVector { get; set; }
        private Point CenterPoint { get; set; }

        private ContentControl DesignerItem
        {
            get
            {
                return DataContext as ContentControl;
            }
        }

        #endregion

        #region Event Handlers

        private void RotateThumb_DragStarted(object sender, DragStartedEventArgs e)
        {
            Canvas canvas = VisualTreeHelper.GetParent(DesignerItem) as Canvas;

            if ((DesignerItem != null) && (canvas != null))
            {
                // The RenderTransformOrigin property of DesignerItem defines transformation center relative to its bounds:
                CenterPoint = DesignerItem.TranslatePoint(
                    new Point(DesignerItem.Width * DesignerItem.RenderTransformOrigin.X,
                        DesignerItem.Height * DesignerItem.RenderTransformOrigin.Y),
                        canvas);

                // Calculate startVector, that is the vector from centerPoint to startPoint:
                Point startPoint = Mouse.GetPosition(canvas);
                StartVector = Point.Subtract(startPoint, CenterPoint);

                // Check if the DesignerItem already has a RotateTransform set...
                RotateTransform rotateTransform = DesignerItem.RenderTransform as RotateTransform;
                if (rotateTransform == null)
                {
                    // If not we create one with zero angle :
                    DesignerItem.RenderTransform = new RotateTransform(0);
                    InitialAngle = 0;
                }
                else
                {
                    InitialAngle = rotateTransform.Angle;
                }
            }
        }

        private void RotateThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            Canvas canvas = VisualTreeHelper.GetParent(DesignerItem) as Canvas;

            if ((DesignerItem != null) && (canvas != null))
            {
                // Calculate deltaVector, that is the vector from centerPoint to current mouse position:
                Point currentPoint = Mouse.GetPosition(canvas);
                Vector deltaVector = Point.Subtract(currentPoint, CenterPoint);

                // Calculate the angle between startVector and dragVector...
                double angle = Vector.AngleBetween(StartVector, deltaVector);

                // ...and update the transformation:
                RotateTransform rotateTransform = DesignerItem.RenderTransform as RotateTransform;
                rotateTransform.Angle = InitialAngle + Math.Round(angle, 0);
            }
        }

        #endregion
    }
}